﻿using System.Collections.Generic;
using GameDesigner.Displayers;
using GameDesigner.Source.Widgets;
using GameLibrary.Model.Core;
using G.Model.Runtime;
using Microsoft.VisualBasic.PowerPacks;
using G.Runtime;

namespace GameDesigner.Source.Displayers
{
    public class GameDisplayerPT : GameDisplayer
    {
        private readonly Game _Game;
        private readonly ButtonPanel _ButtonPanel;
        private readonly MessagePanel _MessagePanel;

        public GameDisplayerPT(Game game, 
                               ShapeContainer container,
                               ButtonPanel pButtonPanel,
                               MessagePanel pMessagePanel
                               ) 
            : base(game.GameDef, container)
        {
            _Game = game;
            _ButtonPanel = pButtonPanel;
            _MessagePanel = pMessagePanel;
            game.GameController.ElementsReadyToMoveChangedEvent += this.OnReadyToMoveChanged;
            game.Elements.Actions.ActionsChangedEvent += this.OnActionChanged;
            game.Elements.PlayerChangedEvent += OnPlayerChangedEvent;
        }

        public Game Game
        {
            get { return _Game; }
        }
        
        public ButtonPanel ButtonPanel
        {
            get { return _ButtonPanel; }
        }
        
        public MessagePanel MessagePanel
        {
            get { return _MessagePanel; }
        }
            
        private void OnPlayerChangedEvent(GameElement subject)
        {
            foreach (ElementDisplayer displayer in Displayers)
            {
                if (displayer.Location == subject.Location)
                {
                    displayer.DisplayIcon();
                    break;
                }
            }
        }

        private void OnReadyToMoveChanged(ICollection<GameElement> elements)
        {
            foreach (SimpleShape shape in base.Shapes.Values)
            {
                shape.SuspendPaint();
                shape.BorderWidth = 1;
            }

            foreach (GameElement e in elements)
            {
                SimpleShape shape = base.Shapes[e.Location];
                shape.BorderWidth = 3;
            }

            foreach (SimpleShape shape in base.Shapes.Values)
            {
                shape.ResumePaint(true);
            }
        }

        private void OnActionChanged()
        {
            foreach (GameLocation location in base.Shapes.Keys)
            {
                SimpleShape shape = Shapes[location];

                if (!_Game.Elements.Actions.Contains(location))
                {
                    shape.FillGradientStyle = FillGradientStyle.None;
                    shape.FillStyle = FillStyle.Transparent;
                }
                else
                {
                    shape.FillStyle = FillStyle.Solid;
                    shape.FillGradientStyle = FillGradientStyle.Horizontal;
                    shape.Refresh();
                }
            }
            
        }

    }
}
